package com.hjt.gulimall.search.exception;

import com.hjt.common.exception.ErrorCodeEnum;
import com.hjt.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;

/**
 * @author:97007
 * @date:2021/9/20 9:32
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.hjt.gulimall.search.controller")
public class ExceptionControllerAdvice {


    /**
     * 处理商品上架异常
     * @param productUpException
     * @return
     */
    @ExceptionHandler(value = {ProductUpException.class})
    public R handleIOException(ProductUpException productUpException) {
        log.error("商品上架失败",productUpException);
        return R.error(ErrorCodeEnum.PRODUCT_UP_ERROR.getCode(), ErrorCodeEnum.PRODUCT_UP_ERROR.getMessage());
    }


    @ExceptionHandler(value = {Exception.class})
    public R handleCommonException(Exception e) {
        log.error("未知异常:{}",e);
        return R.error("未知异常");
    }
}
